/**
 * 
 */
package edu.alaska.uaa.cs401.fgm;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author James
 *
 */
public class FanoronaBoardTest {

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}
	
	/**
	 * Test method for {@link edu.alaska.uaa.cs401.fgm.FanoronaBoard#printBoard()}.
	 */
	@Test
	public void testBoardColors() {
		FanoronaBoardInterface fb = new FanoronaBoard();

		class Tuple {
			public int x,y;
			public FanoronaColor q;
			Tuple(FanoronaColor q,int x,int y) {
				this.q = q; // expected value
				this.x = x; // x
				this.y = y; // y
			}
		}
		ArrayList<Tuple> al = new ArrayList<Tuple>();
		for (int x = 0; x < 8; x++) {
			al.add(new Tuple(FanoronaColor.White,x,0));
			al.add(new Tuple(FanoronaColor.White,x,1));
			if (x == 4) {
				al.add(new Tuple(null,x,2));
			} else if ( x < 4) {
				al.add(new Tuple(((x & 1) == 1)?FanoronaColor.White:FanoronaColor.Black,x,2));
			} else {
				al.add(new Tuple(((x & 1) == 0)?FanoronaColor.White:FanoronaColor.Black,x,2));
			}
			al.add(new Tuple(FanoronaColor.Black,x,3));
			al.add(new Tuple(FanoronaColor.Black,x,4));
		}
		for (Tuple q : al) {
			FanoronaColor retq = fb.getCellColor(q.x, q.y);
			if (retq != q.q) fail(String.format("Invalid piece at %d,%d expected %d got %d",q.x,q.y,q.q.ordinal(),retq.ordinal()));
		}
	}
	
	@Test
	public void testValidateMoveFormat()
	{
		FanoronaBoard game_board = new FanoronaBoard();

		String input = "W 3 2 4 2";
		String[] output = game_board.validateMoveFormat(input);
		try {
			if(output[0].equalsIgnoreCase("W"))
				System.out.println("hi");
			else
			{
				System.out.println(output[0]);
				fail("There was an invalid output from validateMove();, first test");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			fail("There was nothing returned from the function validateMove(); first test");
		}
		
		game_board = new FanoronaBoard();
		input = "W 3 2 4 2 W 4 2 a 2";
		output = game_board.validateMoveFormat(input);
		try {
			if(!output[0].equalsIgnoreCase("Epic Fail"))
				fail("There was an invalid output from validateMove();, second test");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			fail("There was nothing returned from the function validateMove(); second test");
		}
	}
	
	@Test
	public void testValidateMove()
	{
		int i = 1;
		FanoronaBoard game_board = new FanoronaBoard();
		String[] input = new String[5];
		input[0] = new String("W");
		input[1] = new String("3");
		input[2] = new String("2");
		input[3] = new String("4");
		input[4] = new String("2");
		boolean output = game_board.validateMove(input, FanoronaColor.White);
		// valid move, so should return a true, fail if return a false
		if(!output)
			fail("There was an invalid output from validateMove(); "+(i)+" test");
		i++;
		
		game_board = new FanoronaBoard();
		input = new String[10];
		input[0] = new String("W");
		input[1] = new String("3");
		input[2] = new String("2");
		input[3] = new String("4");
		input[4] = new String("2");
		input[5] = new String("W");
		input[6] = new String("4");
		input[7] = new String("2");
		input[8] = new String("3");
		input[9] = new String("2");
		output = game_board.validateMove(input, FanoronaColor.White);
		// invalid move, so should return a false, so it fails if return a true.
		if(output)
			fail("There was an invalid output from validateMove(); "+(i)+" test");
		i++;
		
		game_board = new FanoronaBoard();
		game_board.setPiece(4, 1, 0);
		input = new String[10];
		input[0] = new String("W");
		input[1] = new String("3");
		input[2] = new String("2");
		input[3] = new String("4");
		input[4] = new String("2");
		input[5] = new String("W");
		input[6] = new String("4");
		input[7] = new String("2");
		input[8] = new String("4");
		input[9] = new String("1");
		output = game_board.validateMove(input,FanoronaColor.White);
		// valid move, so should return a true, fail if return a false
		if(!output)
			fail("There was an invalid output from validateMove(); "+(i)+" test");
		i++;
	}
	
	@Test
	public void testBoardLinkage() {
		fail("no test written for linkage yet.");
	}
}
